<template>
  <div class="Detail_correlation" v-if="recommendDate">
    <div class="container">
      <div class="title">相关攻略</div>
      <div class="recommed_list">
        <div
          class="recommed_item"
          v-for="item in recommendDate"
          :key="item.id"
          @click="pushTo(item.id)"
        >
          <div class="left_pic">
            <img :src="item.images[0]" alt v-if="item.images.length" />
            <img src="http://157.122.54.189:9095/assets/images/avatar.jpg" alt v-else />
          </div>
          <div class="right_text">
            <div class="post_title">
              <a href="JavaScrip:;" :title="item.title">{{item.title}}</a>
            </div>
            <div class="post_more">{{item.created_at | format}} 阅读 {{item.watch || 0}}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import moment from "moment";
export default {
  data() {
    return {
      recommendDate: null,
    };
  },
  async mounted() {
    const res = await this.$axios.get("/posts/recommend");
    console.log(res);
    this.recommendDate = res.data.data;
  },
  methods: {
    pushTo(id) {
      this.$router.push({
        path: "/post/detail",
        query: {
          id,
        },
      });
    },
  },
  filters: {
    format(v) {
      return moment(new Date(v)).format("YYYY/MM/DD hh:mm:ss");
    },
  },
};
</script>

<style lang='less' scoped>
.container {
  width: 280px;
  cursor: pointer;
  .title {
    font-weight: 400;
    font-size: 18px;
    padding-bottom: 10px;
    border-bottom: 1px solid #ddd;
  }
  .recommed_item {
    display: flex;
    padding: 20px 0;
    border-bottom: 1px solid #ddd;
  }
  .left_pic {
    width: 100px;
    height: 80px;
    flex-shrink: 0;
    background: #ddd;
    overflow: hidden;
    margin-right: 10px;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .right_text {
    flex: 1;
    position: relative;
    .post_title {
      line-height: 1.4em;
      height: 2.8em;
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      display: -webkit-box;
      overflow: hidden;
      text-overflow: ellipsis;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
    .post_more {
      position: absolute;
      bottom: 0;
      left: 0;
      font-size: 12px;
      color: #999;
    }
  }
}
</style>